﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ConferenceZoom.DTDBService;

namespace ConferenceZoom.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        /// <summary>
        /// Show the default home page
        /// </summary>
        /// <returns></returns>
        [AcceptVerbs("GET")]
        public ActionResult Index(int? number)
        {
            ///
            /// Get the last n conferences that have been rendered and make a list.
            /// TODO: make that 20 part of the configuration.
            /// 

            int lastN = 20;
            if (number.HasValue)
            {
                lastN = number.Value;
            }

            BasicTalkInfo[] talks = null;
            using (DTDBClient tdb = new DTDBClient())
            {
                talks = tdb.GetRenderedTalks(lastN);
            }

            return View(talks);
        }

        /// <summary>
        /// User wants to render a new conference
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        [AcceptVerbs("POST")]
        public ActionResult Index(string url)
        {
            ///
            /// Do some really simple fact checking!
            /// 

            string conferenceID = Models.ConferenceInterface.ConvertURLToUniqueID(url);
            if (conferenceID != null)
            {
                return RedirectToAction("Render", "Conference", new { url = url });
            }

            ///
            /// They failed. So we need to re-show the form...
            /// 

            ViewData.ModelState.AddModelError("url", "Sorry - I didn't recognize the URL as a valid conference URL!");
            return Index ((int?) null);
        }

        /// <summary>
        /// Show the queued conferences - this is not a "normal" thing to do. :-)
        /// </summary>
        /// <returns></returns>
        public ActionResult Queued()
        {
            ///
            /// Collect the talks that aren't rendered yet. We may remove this
            /// feature eventually... :-)
            /// 

            BasicTalkInfo[] talks = null;
            using (DTDBClient tdb = new DTDBClient())
            {
                talks = tdb.GetUnrenderedTalks(20);
            }

            ///
            /// Customize the UW we are going to show
            /// 

            ViewData["Title"] = "DeepZoom Conferences Queued";
            ViewData["Message"] = "DeepZoom Conferences waiting to be rendered (or currently being rendered).";
            return View("Index", talks);
        }

        public ActionResult About()
        {
            ViewData["Title"] = "About Page";

            return View();
        }
    }
}
